﻿/**
 * 远光Gris平台3.0版本 http://www.ygsoft.com/ Grace [Gris Application Cloud-Computing
 * Extension]
 * 
 * 计算器控件
 * 
 * 变更版本：
 * fuxiaopan@ygsoft.com 2015-12-1 创建
 */
define(["jquery","ecp.utils.i18n","calculator"],function($,i18n){
        /***
         * 计算器构造方法。
         */
		$.bsCalculator = function (option){
			var caps = "计算器";
			if(i18n != null &&  i18n.calculator != null && i18n.calculator.calc != null) {
				caps = i18n.calculator.calc;
			}
			var defaultOption = {title:caps,
			                     accuracy:15,movable:true,resizable:false,height:281, width:220,defaultOpen:false,alwaysNew:false};
			this.option = $.extend(defaultOption, option);
			var $body = $("body");
			var calcDom = $body.find("div[ecp='calc']");
			if(this.option.alwaysNew || !calcDom.length) {
				var _id = "Calculator_" + (Math.random() * 1000 + "").substring(0,3);
				this.control = $('<div id="' +_id + '" ecp="calc"></div>');
				$body.append(this.control);
				this.control.css("box-sizing","content-box");
				
				this.name = this.control.attr("name") || this.control.attr("id");
				this.id = this.control.attr("id") || this.control.attr("name");
				render(this);
			} else {
				this.control = calcDom;
				this.control.show();
			}
			return this;
		}
		
		function render(that){
			that.control.calculator(that.option);
	        var left = ($(window).width()-that.control.width())/2+$(document).scrollLeft();
	        var top = ($(window).height()-that.control.height())/2+$(document).scrollTop();
	        that.control.css({position:"absolute",left: left, top: top});
	        that.control.focus();
	        that.control.show();
	        var calcButton = $(".calc-button");
	        calcButton.each(function(){
	        	  $(this).height($(this).parents(".calc-button-wrapper").height()-2);
	        	  $(this).width($(this).parents(".calc-button-wrapper").width()-2);
	        });
	        that.show = show;
	        that.hide = hide;
	        
		}
		
		function show(){
			var ct = this.control;
			ct.show();
			//清除已计算结果（通过模拟点击“C”按钮实现清除）
			var $bds = ct.find("div.calc-buttons-wrapper").children();
			$.each($bds,function(){
				if($(this).children().html() !== "C"){
					return;
				}
				$(this).click();
			})
			//置空
			ct.find("label.calc-text").html("");
			return this;
		}
		
		function hide() {
			this.control.hide();
			return this;
		}
		
});